package com.itfreer.gather.main;

import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.esri.arcgisruntime.data.Feature;
import com.esri.arcgisruntime.data.FeatureTable;
import com.esri.arcgisruntime.layers.FeatureLayer;
import com.itfreer.arcgis.layer.LayerLoader;
import com.itfreer.arcgis.layer.LayerSelect;
import com.itfreer.data.metadata.MetadataService;
import com.itfreer.data.metadata.entity.MetadataEntity;
import com.itfreer.data.metadata.entity.MetadataValue;
import com.itfreer.gather.R;
import com.itfreer.gis.datasyn.entity.ProjectEntity;
import com.itfreer.ui.activity.BaseActivity;

import java.sql.SQLException;
import java.util.Map;

/**
 * 定义属性查看窗口
 */
public class AttributeSeeActivity extends BaseActivity {

    /**
     * 编辑表格
     */
    private LinearLayout main_attribute_table;
    private TextView main_attribute_title;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main_attribute_see_activity);

        main_attribute_table = (LinearLayout) findViewById(R.id.main_attribute_table);
        main_attribute_title = (TextView) findViewById(R.id.main_attribute_title);

        init(LayerSelect.instance.getSelectedFeatures().get(0),
                LayerSelect.instance.getSelectedFeatureLayer());
    }

    /**
     * 回退
     *
     * @param v
     */
    public void back(View v) {
        finish();
    }

    /**
     * 初始化
     */
    private void init(final Feature feature, final FeatureLayer featureLayer){
        Map<String, Object> featureAttributes = feature.getAttributes();
        final FeatureTable featureTable = featureLayer.getFeatureTable();

        // 属性值转换
        Map<String, MetadataValue> attributes = null;
        try {
            ProjectEntity config = LayerLoader.instance.getConfig(featureLayer);
            if(config!=null && config.getMetadata()!=null && !config.getMetadata().equals("")){
                attributes = MetadataService.instance.convert(config.getMetadata(), featureAttributes);
            }else{
                attributes = MetadataService.instance.convert(null, featureAttributes);
            }

            if(config!=null && config.getLayerName()!=null && !config.getLayerName().equals("")){
                main_attribute_title.setText(config.getLayerName());
            }else{
                main_attribute_title.setText(featureLayer.getName());
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }

        int i = 1;
        int c = attributes.size();
        for (String key : attributes.keySet()) {
            String fieldName = key;
            Object fieldValue = attributes.get(key).getValue();
            String dictionaryValue = attributes.get(key).getDictionaryValue();
            if(dictionaryValue!=null && !dictionaryValue.equals("")){
                fieldValue = dictionaryValue;
            }
            MetadataEntity metadata = attributes.get(key).getMetadata();
            String fieldAliseName = fieldName;
            if(metadata!=null && metadata.getAliasName()!=null && !metadata.getAliasName().equals("")){
                fieldAliseName = metadata.getAliasName();
            }

            if(metadata!=null && metadata.getCanShow()!=true){
                c -- ;
                continue;
            }

            LinearLayout tr = new LinearLayout(AttributeSeeActivity.this);
            tr.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT));
            if(i==1){
                tr.setBackgroundResource(R.drawable.shape_top_corner_no_bottom_line);
            } else if (i==c){
                tr.setBackgroundResource(R.drawable.shape_bottom_corner_no_top_line);
            } else {
                tr.setBackgroundResource(R.drawable.shape_no_corner_without_bottom);
            }

            // 字段名称
            final float scale = this.getResources().getDisplayMetrics().density;
            int width = (int) (160 * scale + 0.5f);
            int height = (int) (40 * scale + 0.5f);

            android.widget.TextView field = new android.widget.TextView(AttributeSeeActivity.this);
            field.setLayoutParams(new LinearLayout.LayoutParams(width, height));
            field.setGravity(Gravity.RIGHT|Gravity.CENTER_VERTICAL);
            field.setText(fieldAliseName + "：");
            tr.addView(field);

            // 显示值
            android.widget.TextView value = new android.widget.TextView(AttributeSeeActivity.this);
            value.setTag(key);
            if(fieldValue==null){
                value.setText("");
            }else{
                value.setText(fieldValue.toString());
            }
            LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT, 1);
            layoutParams.setMargins(20, 0, 0, 0);
            value.setLayoutParams(layoutParams);
            value.setGravity(Gravity.LEFT|Gravity.CENTER_VERTICAL);
            tr.addView(value);

            main_attribute_table.addView(tr);
            i ++ ;
        }
    }
}
